<?php

namespace app\api\controller\coll;
use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\coll\Collection;
use app\common\model\coll\Collection as Coll;
use app\common\model\company\Company;
use app\common\model\power\Rolepower;
use app\common\model\user\User as UserModel;

class Selfcoll extends Base
{
    /**
     * 产生jwt加密串，参考session
     */
    public function initialize()
    {
        parent::initialize();
        self::is_login();
        Rolepower::powerguard(203004);
    }

    /**
	 * 获取财务信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if (empty($id)) {
			return myerr('缺少重要参数');
		}
		$data = Coll::user_getinfo($id );
		return myok($data);
	}


    /**
     * 启用
     * @param State int 1：启用，2：禁用
     * @param CollectionId int 当前信息id
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function toggle()
    {
        $param = $this->request->post();
        if (empty($param['CollectionId'])) {
            return myerr('非法请求');
        }
        Collection::where('CompanyId', Collection::SELFCOMPANYID)->update(['State' =>Collection::STATESTOP]);
        Collection::where('CollectionId', $param['CollectionId'])->update(['State' =>Collection::STATEOK]);
        return myok();
    }

    /**
     * 企业账户列表
     * @return \think\response\Json
     */
    public function accountList()
    {
        $data                = input('post.');
        $data                = init_where($data);
        $user_id             = Pub::userid();
        $company             = Company::where('UserId', $user_id)->findOrEmpty();
        $data['CompanyId']   = Collection::SELFCOMPANYID;
        $list                = Collection::usergetlist($data);
        $list['tr']['title'] = ['开户行', '银行卡号', '状态', '持卡人姓名', '联系方式', '设置时间', '用户', '公司'];
        $list['tr']['key']   = ['CardName', 'CardNumber', 'State', 'UserName', 'UserTel', 'UpTime', 'CompanyId', 'UserId'];
//        $list['StateText'] = [1 => '已启用', 2 => '未启用'];
        return myok($list);
    }

    /**
     * 修改或新增财务信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function saveData()
    {
        $data = input('post.');
        unset($data['token']);
        if (!empty($data['CollectionId'])) {
            // 编辑
            $id = $data['CollectionId'];
            unset($data['CollectionId'], $data['CompanyId']);
            $res = Collection::where('CollectionId', $id)->update($data);
            return myok('财务信息修改成功');

        } else {
            // unset($data['CollectionId']);
            // $data['CompanyId'] = Collection::SELFCOMPANYID;
            // $data['State']     = Collection::STATESTOP;
            $res               = Collection::user_create($data,true);
            return myok('财务信息已登记');

        }

    }

    /**
     * 判断账号是否登录
     * @return boolean [description]
     */
    private function is_login()
    {
        $data = UserModel::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
}
